package com.test;

import com.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class test {
    public static void main(String[] args) {

        Student stu1=new Student("nm001","牛马1号",16);
        Student stu2=new Student("nm002","牛马2号",12);
        Student stu3=new Student("nm003","牛马3号",18);
        ArrayList<Student> list=new ArrayList<>();
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);

        Scanner sc=new Scanner(System.in);

        while (true) {
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择：");
            int choice=sc.nextInt();
            switch (choice){
                case 1:
                    addStudent(list);
                    break;
                case 2:
                    removeStudentById(list);
                    break;
                case 3:
                    setStudentInfoById(list);
                    break;
                case 4:
                    queryStudentInfos(list);
                    break;
                case 5:
                    System.out.println("感谢你的使用，再见");
                    System.exit(0);
                default:
                    System.out.println("你的输入有误");
                    break;
            }
        }

    }

    // 查找索引
    private static int getIndex(String id, ArrayList<Student> list) {
        for (int i = 0; i < list.size(); i++) {
            if(id.equals(list.get(i).getId())){
                return i;
            }
        }
        return -1;
    }

    /**
     * 1.添加学生
     */
    private static void addStudent(ArrayList<Student> list) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入学生学号：");
        String id="";
        while (true){
            id=sc.next();
            int index=getIndex(id,list);
            if(index!=-1){
                System.out.println("该学号已经存在,请重新输入");
            }else{
                break;
            }
        }

        System.out.println("请输入学生姓名：");
        String name=sc.next();
        System.out.println("请输入学生年龄：");
        int age=sc.nextInt();
        Student stu=new Student(id,name,age);
        list.add(stu);



    }

    /**
     * 2.删除学生信息
     */
    private static void removeStudentById(ArrayList<Student> list) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入需要删除学生的学号：");
        String id=sc.next();
        int index=getIndex(id,list);
        if(index==-1){
            System.out.println("未查询到改学生，删除失败");
        }else{
            list.remove(index);
            System.out.println("删除成功");
        }
    }

    /**
     * 3.修改学生
     */
    private static void setStudentInfoById(ArrayList<Student> list) {
        System.out.println("请输入需要修改信息的学生学号");
        Scanner sc=new Scanner(System.in);
        String updateId=sc.next();
        int index=getIndex(updateId,list);
        if(index==-1){
            System.out.println("查无此人，修改失败");
        }else {
            System.out.println("请输入新学生姓名：");
            String name=sc.next();
            System.out.println("请输入新学生年龄：");
            int age=sc.nextInt();
            Student stu=new Student(updateId,name,age);
            list.set(index,stu);
        }
    }


    /**
     * 4.查看学生信息
     */
    private static void queryStudentInfos(ArrayList<Student> list) {
        if(list.size()==0){
            System.out.println("查无信息，请添加后重试");
        }else{
            System.out.println("学号\t\t\t姓名\t\t\t年龄");
            for (int i = 0; i < list.size(); i++) {
                Student stu=list.get(i);
                System.out.println(stu.getId()+"\t\t"+stu.getName()+"\t\t"+stu.getAge());
            }
        }
    }


}
